<?php 

$db 	= new Kegiatan();
$table 	= "kegiatan";
$table2 = "kabkota";
$table3 = "sektoral";
$table4 = "monitoring";
$order	= "";
$id 	= htmlentities(mysql_real_escape_string($_GET['id']));

$kegiatan = $db->lihat($table,$table2,$table3,$order,$id);

if ($kegiatan) {
	foreach ($kegiatan as $x) {
	?>
	
		<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
		<script src="../assets/js/polygon.min.js"></script> 
		<script type="text/javascript">
			var peta;
			var gambar_tanda = "../file/icon/<?php echo $x['icon']; ?>"; ;
			
				<?php
					$str = $x['polygon'];
					$str = str_replace(" ","", $str);
					$str = str_replace(")(",") (", $str);
					$str = explode(" ", $str);

					for ($i=0;$i<count($str);$i++) {
						$cord .= "new google.maps.LatLng".$str[$i].",";
					}
					$cord = substr($cord,0,strlen($cord)-1);
				?>
			var paths = [<?php echo $cord; ?>];
			function set_icon(ikon){
				var icon_temp = ikon.split(',');
				var icon_sektoral = icon_temp[1];
				gambar_tanda = "../file/icon/"+icon_sektoral;
			}
			
			function peta_awal(){
				var lokasibaru = new google.maps.LatLng(<?php echo $x['lat'].",".$x['long']; ?>);
				var petaoption = {
					zoom: 13,
					center: lokasibaru,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				
				peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);
				
				poly = new google.maps.Polygon({
					paths: paths,
					strokeWeight: 0,
					fillColor: "#ff0000",
					fillOpacity: 0.5
				});
				poly.setMap(peta);
				tanda = new google.maps.Marker({
					position: lokasibaru,
					map: peta, 
					icon: gambar_tanda,
					draggable : true
				});
				
				 var creator = new PolygonCreator(peta);
				 
				 //reset
				 $('#reset').click(function(){ 
						creator.destroy();
						creator=null;
						creator=new PolygonCreator(peta);
				 });		 
				 
				 
				 //show paths
				 $('#showData').click(function(){ 
						$('#dataPanel').empty();
						if(null==creator.showData()){
							$('#dataPanel').append('Please first create a polygon');
						}else{
							$('#dataPanel').append(creator.showData());
						}
				 });
				 
				google.maps.event.addListener(tanda, 'dragend', function(event){
						document.getElementById('latitude').value = this.getPosition().lat();
						document.getElementById('longitude').value = this.getPosition().lng();
				});
				
				
				
			}
			
			function kasihtanda(lokasi){
				$("#form_lokasi").slideDown();
				$("#latitude").val(lokasi.lat());
				$("#longitude").val(lokasi.lng());
				tanda = new google.maps.Marker({
					position: lokasi,
					map: peta
				});
			}
			
			function setpeta(lokasi){
			var koordinat = lokasi.split(',');
			var x = koordinat[0];
			var y = koordinat[1];
			var id = koordinat[2];
			var lokasibaru = new google.maps.LatLng(x, y);
			var petaoption = {
				zoom: 13,
				center: lokasibaru,
				mapTypeId: google.maps.MapTypeId.ROADMAP
				};
			peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);
			poly = new google.maps.Polygon({
					paths: paths,
					strokeWeight: 0,
					fillColor: "#ff0000",
					fillOpacity: 0.5
				});
			poly.setMap(peta);
			 var creator = new PolygonCreator(peta);
				 
				 //reset
				 $('#reset').click(function(){ 
						creator.destroy();
						creator=null;
						creator=new PolygonCreator(peta);
				 });		 
				 
				 
				 //show paths
				 $('#showData').click(function(){ 
						$('#dataPanel').empty();
						if(null==creator.showData()){
							$('#dataPanel').append('Please first create a polygon');
						}else{
							$('#dataPanel').append(creator.showData());
						}
				 });
			tanda = new google.maps.Marker({
				position: lokasibaru,
				icon: gambar_tanda,
				map: peta, draggable : true
			});

			google.maps.event.addListener(tanda, 'dragend', function(event){
					document.getElementById('latitude').value = this.getPosition().lat();
					document.getElementById('longitude').value = this.getPosition().lng();
			});
		}
		</script> 
		
	
		<div class="row">
			<div class="span8">
				<div class="control-group">
					<div id="map_canvas" style="width:100%; height:500px"></div>
				</div>
			</div>
			<?php
	
		   /* alert yang muncul setelah proses */
	   if (isset($_GET['success']) && ($_GET['success'] == "1")) {

			$o .= '<div class="alert alert-success">
					Proses edit kegiatan berhasil
					</div>';
					
		} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
			
			$o .= '<div class="alert alert-error">
					Proses edit kegiatan gagal
				   </div>';
		}
		
	?>
				<div class="span4">
				<form class="form-horizontal" id="registerHere" method="post" action="?page=editkegiatan&action=edit&id=<?php echo $id; ?>">
				<fieldset>
				<legend>EDIT KEGIATAN</legend>
	
		
	<?php
	  

		
		
	
			$o.= '
				<div class="control-group">
				  <label class="control-label" for="input01">Nama Kegiatan</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" id="kegiatan" value="'.$x['nama_kegiatan'].'" name="kegiatan" rel="popover" data-content="Masukkan nama kegiatan." data-original-title="Kegiatan">
				  </div>
				</div>';
				
							$o .= '		
					
	
			<div class="control-group">
			  <label class="control-label" for="input02">Sektoral</label>
			  <div class="controls">
				<select name="sektoral" onchange="set_icon(this.options[this.selectedIndex].value)" id="sektoral" class="chzn-select" style="width:280px;" tabindex="2">';
		
		
		/* memanggil fungsi untuk menampilkan data sektoral */
		require ('../class/admin/sektoral.php');
		$db3 = new Sektoral();
		$sektoral = $db3->lihat($table3,"idsektoral","");
		/* menampilkan data sektoral */
		$o .= '<option value="'.$x['idsektoral'].",".$x['icon'].'">'.$x['sektoral'].'</option>';
		foreach ($sektoral as $r) {
			$o .= '<option value="'.$r['idsektoral'].",".$r['icon'].'">'.$r['nama_sektoral'].'</option>';
		}
			
	
		$o .= '
				</select>
				</div>
			</div>
				
				<div class="control-group">
				  <label class="control-label" for="input01">Kabupaten</label>
				  <div class="controls">
					<select name="kabupaten" onchange="setpeta(this.options[this.selectedIndex].value)" id="kabupaten" class="chzn-select" style="width:280px;" tabindex="2">';
					/* memanggil fungsi untuk menampilkan data kabupaten */
					require ('../class/admin/kabupaten.php');
					$db2 = new Kabupaten();
					$kabupaten	= $db2->lihat($table2,"idkabkota",""); 
					$o .= '<option value="'.$x['lat'].",".$x['long'].",".$x['idkabkota'].'">'.$x['kabupaten'].'</option>';
					if ($kabupaten) {
						foreach ($kabupaten as $l) {
							$o .= '<option value="'.$l['lat'].",".$l['long'].",".$l['idkabkota'].'">'.$l['nama_kabkota'].'</option>';
						}
					} else {
						$o .= '<option value="">tidak ada data kabupaten</option>';
					}
					
	
			$o .= '
			</select>
					</div>
				</div>
			<div class="control-group">
				<label class="control-label" for="input01">Pagu</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" id="pagu" value="'.$x['pagu'].'" name="pagu" rel="popover" data-content="Masukkan pagu" data-original-title="Pagu">
				  </div>
			</div>
			
			<div class="control-group">
				<label class="control-label" for="input01">Polygon</label>
				  <div class="controls">
					<textarea class="input-xlarge" id="dataPanel" name="polygon"></textarea>
					<button id="showData" type="button" class="btn btn-primary">Tampilkan Koordinat</button>
					<button id="reset" value="Reset" type="button" class="btn btn-warning">Reset Polygon</button>
				  </div>
			</div>
			
			<div class="control-group">
				<label class="control-label" for="input01">Longitude</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" id="longitude" value="'.$x['long'].'" name="longitude" rel="popover" data-content="Masukkan longitude" data-original-title="Longitude">
				  </div>
			</div>
			
			<div class="control-group">
				<label class="control-label" for="input01">Latitude</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" id="latitude" value="'.$x['lat'].'" name="latitude" rel="popover" data-content="Masukkan Latitude" data-original-title="Latitude">
				  </div>
			</div>

			<div class="control-group">
				<label class="control-label" for="input01"></label>
				  <div class="controls">
				   <button type="submit" class="btn btn-success" >Ubah Kegiatan</button>
				  </div>
			</div>
			  </fieldset>
			</form>
			</div>
			</div>
			';
	}
} else {
	$o .= '<div class="alert alert-error">
			Data tidak ditemukan
		    </div>';
	
}

	echo $o;
	

?>
		
		<a href="?page=kegiatan">kembali</a>

<?php


if ($_GET['action'] == "edit") {
	
	$array 		= array();
	$kabupaten 	= $_POST['kabupaten'];
	$array    	= $kabupaten;
	$newarray 	= explode(",", $array);
	
	$array2 	= array();
	$sektoral	= $_POST['sektoral'];
	$array2    	= $sektoral;
	$newarray2 	= explode(",", $array2);
	$id 	= htmlentities(mysql_real_escape_string($_GET['id']));
	
	$nama_kegiatan		= htmlentities(mysql_real_escape_string($_POST['kegiatan']));
	$kabkota			= htmlentities(mysql_real_escape_string($newarray[2]));
	$sektor				= htmlentities(mysql_real_escape_string($newarray2[0]));
	$pagu				= htmlentities(mysql_real_escape_string($_POST['pagu']));
	$polygon       		= $_POST['polygon'];
	$longitude			= htmlentities(mysql_real_escape_string($_POST['longitude']));
	$latitude			= htmlentities(mysql_real_escape_string($_POST['latitude']));
	$tahun				= date('Y');
	$username			= $_SESSION['username'];
	
	$db = new Kegiatan();
	$kegiatan = array ('nama_kegiatan'=>$nama_kegiatan, 
								 'idkabkota'=>$kabkota, 
								 'pagu'=>$pagu, 
								 'lat'=>$latitude, 
								 'long'=>$longitude,
								 'polygon'=>$polygon,
								 'tahun'=>$tahun,
								 'idsektoral'=>$sektor,
								 'username'=>$username);
	
	$ubahkegiatan = $db->ubah($id,$table,$kegiatan);
	
	if ($ubahkegiatan) {
		echo '
			<script language="javascript">
			document.location="?page=editkegiatan&id='.$id.'&success=1";
			</script>
		';
	} else {
		echo '
			<script language="javascript">
			document.location="?page=editkegiatan&id='.$id.'&success=0";
			</script>
		';
	}
	
}





